package com.emm.yixun.mobile.customview

import android.app.Dialog
import android.os.Bundle
import android.support.design.widget.BottomSheetDialogFragment
import android.support.v4.app.FragmentManager
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.emm.yixun.mobile.R
import kotlinx.android.synthetic.main.list_bottom_sheet.view.*

/**
 * Created by he
 * on 2018/5/17.
 */
class BottomSheet : BottomSheetDialogFragment() {

    var data = emptyList<String>()
    var listener: ((Int) -> Unit)? = null

    override fun show(manager: FragmentManager?, tag: String?) {
        if (!isAdded) super.show(manager, tag)
    }


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        val view = View.inflate(context, R.layout.list_bottom_sheet, null).apply {
            tv_cancel.setOnClickListener { dismiss() }
            rv_bottom_sheet.run {
                layoutManager = LinearLayoutManager(context)
                adapter = object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_sheet_content, data) {
                    override fun convert(helper: BaseViewHolder?, item: String?) {
                        helper?.setText(R.id.tv_sheet_content, item)
                    }
                }.apply {
                    setOnItemClickListener { _, _, position ->
                        listener?.invoke(position)
                        dismiss()
                    }
                }
            }
        }
        dialog.setContentView(view)
        return dialog
    }

}